﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Database;
using KPP.Properties;

namespace KPP
{
    public partial class RegisterForm : Form
    {
        public RegisterForm()
        {
            InitializeComponent();
        }

        public RegisterForm(string series, string number)
        {
            InitializeComponent();

            SetDefaults();

            passportSeriesTextBox.Text = series;
            passportNumberTextBox.Text = number;
        }

        private void SetDefaults()
        {
            nameTextBox.SetDefaults();
            midnameTextBox.SetDefaults();
            surnameTextBox.SetDefaults();
            placeOfPassportGetTextBox.SetDefaults();

            nameTextBox.DefaultText = Resources.defaultName;
            midnameTextBox.DefaultText = Resources.defaultMidname;
            surnameTextBox.DefaultText = Resources.defaultSurname;
            placeOfPassportGetTextBox.DefaultText = Resources.defaultPassIssuePlace;
        }

        private void registerTimer_Tick(object sender, EventArgs e)
        {

        }

        private void RegisterForm_Load(object sender, EventArgs e)
        {
            registerTimer.Start();
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (!nameTextBox.HasText || !midnameTextBox.HasText || !surnameTextBox.HasText)
            {
                MessageBox.Show(Resources.NoDataError, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string firstName = nameTextBox.Text;
            string middleName = midnameTextBox.Text;
            string lastName = surnameTextBox.Text;

            DateTime birthDate = DateTime.ParseExact(birthDatePicker.Text, "dd.MM.yyyy", null);

            int passSeries = Convert.ToInt32(passportSeriesTextBox.Text);
            int passNumber = Convert.ToInt32(passportNumberTextBox.Text);

            if (!File.Exists(imageTextBox.Text))
            {
                MessageBox.Show(Resources.PhotoPathError, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Image image;
            try
            {
                image = Image.FromFile(imageTextBox.Text);
            }
            catch (OutOfMemoryException)
            {
                MessageBox.Show(Resources.PhotoError, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            var person = new Person(firstName, middleName, lastName, birthDate, passSeries, passNumber, image);

            DatabaseHelper.InsertNewPerson(person);

            MessageBox.Show(Resources.Success, Resources.Result, MessageBoxButtons.OK);

            Close();
        }

        private void chooseImageButton_Click(object sender, EventArgs e)
        {
            DialogResult dialogResult = imageDialog.ShowDialog();

            if (dialogResult == DialogResult.OK)
            {
                string fileName = imageDialog.FileName;
                imageTextBox.Text = fileName;
            }
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
